﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TreeViewPicker
{
    internal class OrganisationUnitService
    {

        public List<OrganisationalUnit> RetrieveOrganisationUnitFor(string id)
        {
            if (id == null) throw new ArgumentNullException("id");
            List<Department> deps=null;
            if (id != String.Empty)
            {
                deps = new Data().GetById(id);
            }
            else
            {
                deps = new Data().GetTop();
            }
            return Transform(deps);
        }

        private List<OrganisationalUnit> Transform(List<Department> deps)
        {
            var ou = new List<OrganisationalUnit>();
            if (deps != null)
            {
                foreach (var item in deps)
                {
                    ou.Add(new OrganisationalUnit { Name = item.Name, CName = item.Name });
                }
            }
            return ou;
        }
    }

    public class Data
    {
        private static List<Department> Departments ;

        public Data()
        {
            if (Departments == null)
            {
                Departments = new List<Department>();
                Departments.Add(MakeProduction());
                Departments.Add(MakeMarketing());
                Departments.Add(MakeSales());
                Departments.Add(MakeIT());
                Departments.Add(MakeFinance());
            }
        }

        private static Department MakeFinance()
        {
            Department dept = new Department() { Name = "Finance" };
            return dept;
        }

        private static Department MakeIT()
        {
            Department dept = new Department() { Name = "IT" };
            return dept;
        }

        private static Department MakeMarketing()
        {
            Department dept = new Department() { Name = "Marketing" };
            return dept;
        }

        private static Department MakeSales()
        {
            Department dept = new Department() { Name = "Sales" };
            return dept;
        }

        private static Department MakeProduction()
        {
            Department production = new Department() { Name = "Production" };
            var prdDepts = new List<Department>() 
            {
                new Department() { Name = "Production1" },
                new Department() { Name = "Production2" },
                new Department() { Name = "Production3" },
                new Department() { Name = "Production4" },
                new Department() { Name = "Production5" }

            };
            production.Departments = prdDepts;
            return production;
        }

        public List<Department> GetById(string id)
        {
            var dep =  Departments.SingleOrDefault(c => c.Name.ToUpperInvariant() == id.ToUpperInvariant());
            return dep.Departments;
        }

        public List<Department> GetTop()
        {
            return Departments;
        }

    }

    public class Department
    {
        public string Name  {get;set;}
        public List<Department> Departments {get;set;}

    }



    

}
